import 'dart:io';

import 'package:extended_image/extended_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../res.dart';

class CustomCacheImage extends StatelessWidget {
  final String? imageUrl;
  final bool isCircle;
  final double radius;
  final Widget? placeHolder;
  final Widget? errorWidget;
  final String? holderImagePath;
  final String? errorImagePath;
  final double? height;
  final double? width;
  final BoxFit fit;
  final double resize;
  final Color? color;
  final BoxBorder? boxBorder;
  final ExtendedImageMode? extendedImageMode;
  final BorderRadius? borderRadius;

  const CustomCacheImage(
      {Key? key,
      required this.imageUrl,
      this.isCircle = false,
      this.radius = 0,
      this.borderRadius,
      this.errorImagePath,
      this.holderImagePath = Res.common_gender_man,
      this.placeHolder,
      this.errorWidget,
      this.height,
      this.width,
      this.fit = BoxFit.cover,
      this.color,
      this.boxBorder,
      this.extendedImageMode,
      this.resize = 0})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return _buildImageField();
  }

  Widget _buildImageField() {
    return _buildCacheImageField();
  }

  Widget? loadStateChanged(ExtendedImageState state) {
    switch (state.extendedImageLoadState) {
      case LoadState.loading:
        if (placeHolder != null) {
          return placeHolder!;
        } else if (holderImagePath != null) {
          return getAssetsImage(holderImagePath!, fit,
              width: width, height: height);
        } else {
          return getAssetsImage(Res.common_gender_man, fit,
              width: width, height: height);
        }
      case LoadState.completed:
        return state.completedWidget;
      case LoadState.failed:
        if (errorWidget != null) {
          return errorWidget!;
        } else if (errorImagePath != null) {
          return getAssetsImage(errorImagePath!, BoxFit.contain,
              width: width, height: height);
        } else {
          return getAssetsImage(Res.common_gender_man, fit,
              width: width, height: height);
        }
    }
  }

  Widget _buildCacheImageField() {
    String fullImageUrl =
        (imageUrl != null && resize > 0 ? "$imageUrl?w=$resize" : imageUrl) ??
            '';
    if (fullImageUrl.contains('gender')) {
      fullImageUrl = Res.common_gender_man;
    }
    BorderRadius? borderRadius =
        radius != 0 ? BorderRadius.all(Radius.circular(radius)) : null;

    if (fullImageUrl.startsWith("http")) {
      return ExtendedImage.network(
        fullImageUrl,
        borderRadius: this.borderRadius ?? borderRadius,
        enableLoadState: true,
        loadStateChanged: loadStateChanged,
        height: height,
        width: width,
        fit: fit,
        shape: isCircle ? BoxShape.circle : BoxShape.rectangle,
        border: boxBorder,
        mode: extendedImageMode ?? ExtendedImageMode.none,
        cache: true,
      );
    } else if (fullImageUrl.contains('assets/images/')) {
      return ExtendedImage.asset(
        fullImageUrl,
        borderRadius: this.borderRadius ?? borderRadius,
        enableLoadState: true,
        loadStateChanged: loadStateChanged,
        height: height,
        width: width,
        color: color,
        border: boxBorder,
        shape: isCircle ? BoxShape.circle : BoxShape.rectangle,
        fit: fit,
      );
    } else {
      return ExtendedImage.file(
        File(fullImageUrl),
        borderRadius: this.borderRadius ?? borderRadius,
        enableLoadState: true,
        loadStateChanged: loadStateChanged,
        height: height,
        width: width,
        border: boxBorder,
        shape: isCircle ? BoxShape.circle : BoxShape.rectangle,
        fit: fit,
      );
    }
  }

  static Image getFileImage(File file, BoxFit boxFit) {
    return Image.file(file, fit: boxFit);
  }

  static Image getAssetsImage(String imageName, BoxFit fit,
      {width, height, color}) {
    return Image.asset(imageName,
        fit: fit, width: width, height: height, color: color);
  }
}
